<template>
  <div>
    <el-dialog
      :title="drawTitle"
      :visible="dialogVisible"
      @close="closeFn"
    >
      <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" placeholder="请选择部门名称" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formData.code" placeholder="请选择部门编码" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="formData.manager" placeholder="请选择部门负责人" @focus="getemployFn">
            <el-option v-for="item in peoples " :key="item.id" :label="item.username" :value="item.username" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="formData.introduce"
            autosize
            maxlength="30"
            placeholder="请选择部门介绍"
            show-word-limit
            size="medium"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row justify="center" type="flex">
          <el-button size="mini" type="primary" @click="submit">确定</el-button>
          <el-button size="mini">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments } from '@/api/table'
import { getEmployeeSimple } from '@/api/getemployees'
import { addDepartments, getdepartDetail, updateDetail } from '@/api/departments'

export default {
  name: 'DiaLog',
  props: {
    dialogVisible: { type: Boolean, default: false },
    treeNode: { type: Object, default: () => ({}) }
  },
  data() {
    // 名称校验 同级部门名字不能相同
    const validatname = async(rules, value) => {
      const { depts } = await getDepartments()
      let findSon
      if (this.formData.id) {
        findSon = depts.filter(item => item.pid === this.treeNode.pid && item.id !== this.treeNode.id)
      } else {
        findSon = depts.filter(item => item.pid === this.treeNode.id)
      }
      // 校验返回值有问题
      const result = findSon.some(item => item.name === value)
      console.log(result)
      if (result) {
        return Promise.reject()
      }
    }
    // 所有任何部门code不能相同 code唯一
    const validatcode = async(rules, value) => {
      const { depts } = await getDepartments()
      console.log(this.treeNode.code)
      let result
      if (this.formData.id) {
        result = depts.some(item => item.code === value && item.id !== this.treeNode.id)
      } else {
        result = depts.some(item => item.code === value)
      }
      if (result) {
        return Promise.reject(new Error('重复了'))
      }
    }

    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      peoples: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          {
            validator: validatname,
            trigger: 'blur'
          }
        ],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          {
            validator: validatcode,
            trigger: 'blur'
          }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  computed: {
    drawTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    closeFn() {
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      // this.$refs.form.resetFields()
      this.$emit('update:dialogVisible', false)
    },
    async getemployFn() {
      const res = await getEmployeeSimple()
      this.peoples = res
    },
    async submit() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        updateDetail(this.formData)
      } else {
        await addDepartments({
          ...this.formData, pid: this.treeNode.id
        })
      }
      this.$message.success(this.formData.id ? '编辑成功' : '新增成功')
      this.closeFn()
      this.$emit('getdepart')
    },
    async getDepartDetail(id) {
      // console.log(id)
      const res = await getdepartDetail(id)
      console.log(res)
      this.formData = res
    }
  }

}
</script>

<style lang='scss' scoped></style>
